﻿using UnityEngine;
using UnityEngine.UI;

public class GameMain : MonoBehaviour
{
    public TowerController towerController;
    public PlayerController playerController;

    public GameObject menuUI;
    public GameObject beginUI;
    public GameObject stopButton;
    public GameObject OverUI;
    public GameObject scoreRecord;
    public ScoreManager scoreManager;
    public MonsterManager monsterManager;
    public GameObject beginDescription;
    public GameObject startButton;
    public GameObject exitButton;

//    // Use this for initialization
    void Start()
    {
        menuUI.SetActive(false);
        stopButton.SetActive(false);
        scoreManager.resetScore();
        towerController.setBloodVisble(false);
        scoreRecord.SetActive(false);
        beginDescription.GetComponent<Text>().text = "欢迎你来到一个搞笑的世界";
        
        startButton.GetComponentInChildren<Text>().text = "进去看看";
        exitButton.GetComponentInChildren<Text>().text = "进都不想进，拜拜";
    }


    void Update()
    {
        scoreRecord.GetComponent<Text>().text = "你干掉了" + scoreManager.getScore() + "只怪东西";
    }


    // 开始游戏
    public void beginGame()
    {
        // 重置塔
        towerController.towerRebirth();
        // 重置人
        // playerController.resetPlayer();
        // 设置游戏状态为进行中
        GameStateManager.GameState = GameState.Playing;
        
        stopButton.SetActive(true);
        playerController.resetPlayer();
        scoreManager.resetScore();
        towerController.setBloodVisble(true);
        scoreRecord.SetActive(true);

    }

    // 暂停游戏
    public void stopGame()
    {
        GameStateManager.GameState = GameState.Stop;
        menuUI.SetActive(true);
        stopButton.SetActive(false);
        playerController.changeMovable(false);
    }

    // 继续游戏
    public void continueGame()
    {
        GameStateManager.GameState = GameState.Playing;
        menuUI.SetActive(false);
        stopButton.SetActive(true);
        playerController.changeMovable(true);
    }

    // 返回主界面
    public void returnBegin()
    {
        beginUI.SetActive(true);
        GameStateManager.GameState = GameState.Begin;
        stopButton.SetActive(false);
        playerController.initPlayer();
        OverUI.SetActive(false);
        towerController.setBloodVisble(false);
        monsterManager.clearAllMonster();
        
        if (towerController.getTowerHP() <= 0)
        {
            if (scoreManager.getScore() > 0)
            {
                beginDescription.GetComponent<Text>().text = "上一次你干掉了" + scoreManager.getScore() + "只怪东西，好玩吗？要不要再来一把！";
            } else
            {
                beginDescription.GetComponent<Text>().text = "上一次你一只怪东西都杀不掉，你比我还搞笑。。。";
            }
        } else
        {
            if (scoreManager.getScore() > 0)
            {
                beginDescription.GetComponent<Text>().text = "上一次你干掉" + scoreManager.getScore() + "只怪东西后立地成佛，然后把塔掀了，还想再来一把吗？";
            } else
            {
                beginDescription.GetComponent<Text>().text = "上一次你一只怪东西都没杀掉就无念无想地回到这里了";
            }
        }
       

        startButton.GetComponentInChildren<Text>().text = "好，再来！";
        exitButton.GetComponentInChildren<Text>().text = "不玩了，垃圾游戏";

    }
    
    public void overGame()
    {
        GameStateManager.GameState = GameState.Over;
        menuUI.SetActive(false);
        var score = scoreManager.getScore();
        playerController.changeMovable(false);
        OverUI.SetActive(true);
        if (towerController.getTowerHP() <= 0)
        {
            OverUI.transform.Find("ScoreText").GetComponent<Text>().text = "你真的很努力在防守，抵挡住了" + score + "只怪东西！"+ "但很可惜，被偷家了!";
        } else
        {
            OverUI.transform.Find("ScoreText").GetComponent<Text>().text = "干掉" + score + "只怪东西后，你悟了\n于是把自己家的塔掀了";
        }
        scoreRecord.SetActive(false);
    }
}
